<?php


namespace App\Models;


use App\Http\Constants\ApplicationStatusConstants;
use Illuminate\Database\Eloquent\Model;

class CoinsType extends Model
{
    protected $table = 'life_coins_type';
    protected $appends = ['status_name'];
    protected $attributes = [
        'status' => ApplicationStatusConstants::START,
    ];

    protected $fillable = [
        'id',
        'type_code',
        'type_name',
        'parent_code',
        'type',
        'subsidy',
        'level',
        'status',
        'created_at',
        'updated_at',
    ];

    protected $casts = [
        'type_code' =>'string',
        'type_name' =>'string',
    ];

    protected function getStatusNameAttribute()
    {
        $attributes = $this->attributes;
        $value = array_key_exists('status', $attributes) ? $attributes['status'] : null;
        if (is_null($value)) return null;
        $map = array_column(ApplicationStatusConstants::STATUS,'label', 'value');
        return array_key_exists($value, $map) ? $map[$value] : null;

    }

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

}
